package com.ichi2.anki.pj2;

import android.app.Activity;

import com.ichi2.anki.R;
import com.ichi2.anki.preferences.Preferences;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.espresso.action.ViewActions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.pressBack;
import static androidx.test.espresso.action.ViewActions.clearText;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.pressKey;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.action.ViewActions.swipeDown;
import static androidx.test.espresso.action.ViewActions.swipeUp;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;


/**
 * Basic tests showcasing simple view matchers and actions like {@link ViewMatchers#withId},
 * {@link ViewActions#click} and {@link ViewActions#typeText}.
 * <p>
 * Note that there is no need to tell Espresso that a view is in a different {@link Activity}.
 * 连接模拟器：nox_adb.exe connect 127.0.0.1:62001
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class PreferencesTest {
    @Rule
    public ActivityScenarioRule<Preferences> activityScenarioRule = new ActivityScenarioRule<>(Preferences.class);


    /**
     * 搜索设置
     */
    @Test
    public void search() {
        onView(withId(R.id.preferences_search)).perform(click());
        onView(withId(R.id.search_src_text)).perform(typeText("123"));
        onView(withText("No results")).check(matches(isDisplayed()));

        onView(withId(R.id.search_src_text)).perform(clearText(), typeText("the"));

        onView(withText("Theme")).perform(click());
        onView(withText("Theme")).check(matches(isDisplayed()));
    }


    /**
     * 语言设置
     */
    @Test
    public void language() {
        onView(withText("General")).perform(click());
        onView(withText("Language")).perform(click());
        onView(withText("English")).perform(swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown(), swipeDown());

        onView(withText("System language")).check(matches(isDisplayed()));
    }


    /**
     * 错误报告
     */
    @Test
    public void error() {
        onView(withText("General")).perform(click());
        onView(withText("General")).perform(click());
        onView(withText("Error reporting mode")).perform(click());
        onView(withText("Always report")).perform(click());

        onView(withText("Always report")).check(matches(isDisplayed()));
    }


    /**
     * 错误报告
     */
    @Test
    public void check() {
        onView(withText("General")).perform(click());
        onView(withText("Error reporting mode")).perform(click());
        onView(withText("Always report")).perform(click());

        onView(withText("Always report")).check(matches(isDisplayed()));
    }


    /**
     * 学习方式
     */
    @Test
    public void studying() {
        onView(withText("General")).perform(click());
        onView(withText("Deck for new cards")).perform(click());
        onView(withText("Use current deck")).perform(click());

        onView(withText("Use current deck")).check(matches(isDisplayed()));
    }


    /**
     * 设置复习
     */
    @Test
    public void new_card_position() {
        onView(withText("Reviewing")).perform(click());
        onView(withText("New card position")).perform(click());
        onView(withText("New cards after reviews")).perform(click());

        onView(withText("New cards after reviews")).check(matches(isDisplayed()));
    }


    /**
     * 学习时间限制
     */
    @Test
    public void learn_ahead_limit() {
        onView(withText("Reviewing")).perform(click());
        onView(withText("Learn ahead limit")).perform(click());
        onView(withText("Learn ahead limit")).check(matches(isDisplayed()));
        onView(withId(16908313)).perform(click());
    }


    /**
     * 时间盒修改
     */
    @Test
    public void timebox() {
        onView(withText("Reviewing")).perform(click());
        onView(withText("Timebox time limit")).perform(click());
        onView(withText("Timebox time limit")).check(matches(isDisplayed()));
        onView(withId(16908313)).perform(click());
    }


    /**
     * 检查账户
     */
    @Test
    public void account() {
        onView(withText("Sync")).perform(click());
        onView(withId(R.id.preferences_search)).perform(click());
        onView(withId(R.id.search_src_text)).perform(typeText("s"));
        onView(withText("Sync")).perform(click());
        onView(withText("AnkiWeb account")).perform(click());
        onView(withText("Log in")).check(matches(isDisplayed()));
    }


    /**
     * fetch media
     */
    @Test
    public void fetch_media() {
        onView(withText("Sync")).perform(click());
        onView(withText("Fetch media on sync")).perform(click());
        onView(withText("Never")).perform(click());
        onView(withText("Never")).check(matches(isDisplayed()));
    }


    /**
     * 通知管理
     */
    @Test
    public void notify_when() {
        onView(withText("Notifications")).perform(click());
        onView(withText("Notify when")).perform(click());
        onView(withText("Never notify")).perform(click());
        onView(withText("Never notify")).check(matches(isDisplayed()));
    }


    /**
     * 外观设置
     */
    @Test
    public void appearance() {
        onView(withText("Appearance")).perform(click());
        onView(withText("Theme")).perform(click());
        onView(withText("Dark")).perform(click());
        onView(withText("Dark")).check(matches(isDisplayed()));
    }


    /**
     * 按钮设置
     */
    @Test
    public void button() {
        onView(withText("Appearance")).perform(click());
        onView(withText("App bar buttons")).perform(click());
        onView(withText("Undo")).perform(click());
        onView(withText("Show if room")).perform(click());

        onView(withText("Reschedule")).perform(click());
        onView(withText("Disabled")).perform(click());

        onView(withText("Flag")).perform(click());
        onView(withText("Disabled")).perform(click());

        onView(withText("Edit note")).perform(click());
        onView(withText("Disabled")).perform(click());

        onView(withText("Edit tags")).perform(click());
        onView(withText("Disabled")).perform(click());

        onView(withText("Add note")).perform(click());
        onView(withText("Disabled")).perform(click());

        onView(withText("Replay audio")).perform(click());
        onView(withText("Disabled")).perform(click());

        onView(withText("Show if room")).check(matches(isDisplayed()));
    }


    /**
     * 全屏模式
     */
    @Test
    public void fullscreen() {
        onView(withText("Appearance")).perform(click());
        onView(withText("Fullscreen mode")).perform(click());
        onView(withText("Off")).perform(click());
        onView(withText("Off")).check(matches(isDisplayed()));
    }


    /**
     * 控制设置
     */
    @Test
    public void controls() {
        onView(withText("Controls")).perform(click());
        onView(withText("Enable gestures")).perform(click());
        onView(withText("9-point touch")).perform(click());
        onView(withText("Full screen navigation drawer")).perform(click());
        onView(withText("Show answer")).perform(click());
        onView(withText("Add key")).perform(click());
        onView(withText("Press a key")).perform(pressKey(27));
        onView(withText("OK")).perform(click());

        onView(withText("Add key")).check(doesNotExist());
    }


    /**
     * 权限设置
     */
    @Test
    public void access() {
        onView(withText("Accessibility")).perform(click());
        onView(withText("Card zoom")).perform(click());

        pressBack();
        onView(withText("Image zoom")).perform(click());
        pressBack();
        onView(withText("Answer button size")).perform(click());
        pressBack();
        onView(withText("Show large answer buttons")).perform(click());
        onView(withText("Card browser font scaling")).perform(click());
        pressBack();

        onView(withText("100")).check(doesNotExist());
    }


    /**
     * 高级设置
     */
    @Test
    public void advanced() {
        onView(withText("Advanced")).perform(click());
        onView(withText("AnkiDroid directory")).perform(click());

        onView(withText("OK")).perform(click());
        pressBack();

        onView(withText("Max number of backups")).perform(click());
        onView(withText("OK")).perform(click());

        onView(withText("Text to speech")).perform(click());

        onView(withText("Reset languages")).perform(click());
        onView(withText("OK")).perform(click());
        onView(withText("All states reset")).check(matches(isDisplayed()));

        onView(withText("Chess notation support")).perform(click());

        onView(withText("Double scrolling")).perform(click());
    }


    /**
     * 开发者选项
     */
    @Test
    public void developer_options() {
        onView(withText("Advanced")).perform(swipeUp(), swipeUp(), swipeUp(), swipeUp(), swipeUp(), swipeUp(), swipeUp());
        onView(withText("Developer options")).perform(scrollTo(), click());
        onView(withId(R.id.settings_container)).check(matches(isDisplayed()));
    }


    /**
     * 关于
     */
    @Test
    public void about() {
        onView(withText("Advanced")).perform(swipeUp(), swipeUp(), swipeUp(), swipeUp(), swipeUp(), swipeUp(), swipeUp());
        onView(withText("About")).perform(click());
        onView(withId(R.id.about_app_logo)).check(matches(isDisplayed()));
    }
}
